<?php
// +----------------------------------------------------------------------
// | 车钉钉 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: YagamiZN
// +----------------------------------------------------------------------

namespace app\admin\controller;

/**
 * 车系控制器
 */
class Category extends Base{
    protected $brandList;//品牌
    /**
     * 前置操作方法列表
     * @var array $beforeActionList
     * @access protected
     */
    protected $beforeActionList = ['baseInfo'];
    /**
     * 构造方法，初始化站点信息
     */
    protected function _initialize(){
        parent::_initialize();
    }
    //模版信息初始化
    public function baseInfo(){
        $baseWebmsg=cache('baseWebMsg');
        $this->assign('baseWebMsg',$baseWebmsg);
        $brandList=db('car_brand')->select();
        $this->brandList=$brandList;
        $this->assign('brandList',$brandList);
    }
    
	//菜单列表
    public function index(){
        return $this->fetch();
    }
    //添加车系页面
    public function categoryAdd(){
        $id=input('param.id');//品牌id
        if(isset($id)){
            $cateone=db('car_brand')->where('id',$id)->find();
            $this->assign('cateone',$cateone);
        }
        return $this->fetch();
    }
    //新增、更新操作 
    public function categorySave(){
        $id=input('param.id/d');//有id则为更新操作
        $brand=input('post.brand/d');//品牌id
        $name=input('post.name');
        $rank=input('post.rank/d');
        $mark=input('post.mark','','strip_tags,htmlspecialchars');
        $isdisplay=input('post.isdisplay/d');
        if(empty($name)||empty($rank)||empty($brand)){
            return ['code'=>300,'msg'=>'*为必填项'];
        }elseif (!empty($id)){
            $data=['name'=>$name,'pid'=>$brand,'rank'=>$rank,'text'=>$mark,'create_time'=>time(),'isdisplay'=>$isdisplay];
            $isOK=db('car_brand')->where('id',$id)->update($data);
            if($isOK){
                return ['code'=>200,'msg'=>'修改成功'];
            }else{
                return ['code'=>400,'msg'=>'修改失败'];
            }
        }
        $isName=db('car_brand')->where('name',$name)->find();
        if(!empty($isName)){
            return ['code'=>300,'msg'=>'车系已存在'];
        }
        $data=['name'=>$name,'pid'=>$brand,'rank'=>$rank,'text'=>$mark,'create_time'=>time(),'isdisplay'=>$isdisplay];
        $isOK=db('car_brand')->insert($data);
        if($isOK){
            return ['code'=>200,'msg'=>'提交成功'];
        }else{
            return ['code'=>400,'msg'=>'提交失败'];
        }
    }
}
